package com.wu.leetcode.leetcode2;

/**
 * Leetcode2-两数之和
 */
class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //(2 -> 4 -> 3) + (5 -> 6 -> 4)  => 7 -> 0 -> 8
        ListNode head = new ListNode(-1);
        ListNode cur = head;
        int tem = 0;
        while (l1 != null || l2 != null){
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;

            int num = x + y + tem;

            tem = num / 10;
            num = num % 10;
            cur.next = new ListNode(num);
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
            cur = cur.next;
        }
        if (tem == 1){
            cur.next = new ListNode(tem);
        }
        return head.next;
    }
}
